import axios from '@/libs/api.request'
import StringUtil from '@/libs/string-util'

const api_module = 'systemDictData'
/**
 * 所有部门列表
 */
const list = (number, size, queryObj) => {
  number = !isNaN(number) ? number : 1
  size = !isNaN(size) ? size : 10
  queryObj = !(typeof queryObj === 'undefined') ? queryObj : {}
  // 请求函数进行处理
  // array转字符串
  // 空字符直接丢掉
  if (Object.keys(queryObj).length > 0) {
    const keys = Object.keys(queryObj)
    for (let i = 0; i < keys.length; i++) {
      const k = keys[i]
      const item = queryObj[k]
      // 解析数组
      if (item instanceof Array) {
        queryObj[k] = item.join(',')
      }
      // 解析字符串
      if (typeof item === 'string' && StringUtil.isEmpty(item)) {
        delete queryObj[k]
      }
    }
  }
  // const params = Object.assign(queryObj, { pageSize: size, currentPage: number })
  const params = Object.assign(queryObj)

  return axios.request({
    url: `api/${api_module}`,
    method: 'get',
    params: params
  })
}

/**
 * 删除单个用户
 */
const remove = (id) => {
  return axios.request({
    url: `api/${api_module}`,
    method: 'delete',
    params: {
      id: id
    }
  })
}

/**
 * 删除多个用户
 * @param idList list
 */
const removeBatch = (idList) => {
  return axios.request({
    url: `api/${api_module}/batch`,
    method: 'delete',
    data: {
      uuidList: idList
    }
  })
}

/**
 * 新增用户
 */
const add = (data) => {
  return axios.request({
    url: `api/${api_module}`,
    method: 'post',
    data: data
  })
}

/**
 * 更新用户
 */
const update = (data) => {
  return axios.request({
    url: `api/${api_module}`,
    method: 'put',
    data: data
  })
}

/**
 * 根据字典类型查找列表
 */
const getListByDataType = (dataType) => {
  return axios.request({
    url: `api/${api_module}/data_type/${dataType}`,
    method: 'get'
  })
}

export default {
  update,
  add,
  list,
  remove,
  removeBatch,
  getListByDataType
}
